package com.wjt.mylibrary.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

@Suppress("UNCHECKED_CAST")
abstract class BaseFragment:Fragment() ,View.OnClickListener{
    init {
        Log.i("test", "BaseFragment")
    }
    private val  TAG = javaClass.simpleName
    private var isLoaded = false

    /**
     * 根view
     */
    lateinit var mRootView: View

    abstract fun lazyInit()
    //抽象的方法,必须实现
    abstract fun setNowActivityName():String
    abstract fun setLayout():Int
    abstract fun initView()
    abstract fun initData()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.i("test", "在" + setNowActivityName() + "Fragment onAttach里面")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("test", "在" + setNowActivityName() + "Fragment onCreate 里面")
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i("test", "在" + setNowActivityName() + "Fragment onCreateView 里面")
        mRootView = inflater.inflate(setLayout(),container,false)
        initView()
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        Log.i("test", "在" + setNowActivityName() + "Fragment onViewCreated 里面")
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.i("test", "在" + setNowActivityName() + "Fragment onActivityCreated 里面")
    }

    override fun onStart() {
        super.onStart()
        Log.i("test", "在" + setNowActivityName() + "Fragment onStart 里面")
    }

    override fun onResume() {
        Log.i("test", "在" + setNowActivityName() + "Fragment onResume里面")
        super.onResume()
        if (!isLoaded && !isHidden) {
            lazyInit()
            Log.d(TAG, "lazyInit:!!!!!!!")
            isLoaded = true
        }
    }
    override fun onPause() {
        super.onPause()
        Log.i("test", "在" + setNowActivityName() + "Fragment onPause 里面")
    }

    override fun onStop() {
        super.onStop()
        Log.i("test", "在" + setNowActivityName() + "Fragment onStop 里面")
    }

    override fun onDestroyView() {
        Log.i("test", "在" + setNowActivityName() + "Fragment onDestroyView 里面")
        super.onDestroyView()
        isLoaded = false
    }

    override fun onDestroy() {
        super.onDestroy()
    }
    override fun onDetach() {
        super.onDetach()
        Log.i("test", "在" + setNowActivityName() + "Fragment onDetach 里面")
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.i("test", "在" + setNowActivityName() + "Fragment onHiddenChanged 里面")
    }
    public fun getRootView():View {
        return mRootView
    }
}